// pages/me/index.js
import {navTo,navToTab,onInput,onClick,showText} from '../../utils/reuse'
import {login,users,getStore,inqTel,getTel,getShop,getshopCollectListCount} from '../../api/index'
import {baseURL} from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  // users()
  data: {

    // 店家端true 用户端false
    hotel_show:false,
    // 0未登录 1未绑定手机号 2都完成了
    loginval:0,
    // 店铺信息
    shop_obj:{},
    storeShow:false,
    admin:{},
    all_admin:false,
    admin2:false,
    admin3:false,

    session_key:'',
    openid:0,
    userInfo:{},
    cartTotal:0,
    shop_count:0,
    baseURL
  },
  // 扫一扫
  scanFn(){
    wx.scanCode({
      success(res){
        console.log(res)
      }
    })
  },
  //个人信息
  qrFn(){
    wx.navigateTo({
      url: '/pages/set/index?id=' + 1 ,
    })
    wx.setStorageSync('userInfo', this.data.userInfo)

  },
  //个人信息
  qrFn1(){
    wx.navigateTo({
      url: '/pages/set/index?id=' + 2,
    })
    wx.setStorageSync('shop_obj', this.data.shop_obj)
  },
  // 登录
  loginFn(){
    const that = this
    showText('登录中...','3')
    wx.getUserProfile({
      desc: '展示用户信息', 
      success: (res) => {
        var obj = {
          signature:res.signature,
          rawData:res.rawData,
          session_key:that.data.session_key,
          openid:that.data.openid,
        }
        users(obj).then(res=>{
          if(res.sta == 1){
            that.setData({
              loginval:1,
              userInfo:res.user
            })
            if(res.uid){
              wx.setStorageSync('uid', res.uid)
            }
            var uid = wx.getStorageSync('uid')
            inqTel(uid).then(res=>{
              if(res.sta == 0){
                showText('用户信息有误！', 2)
              }else if(res.sta == 1){
                that.setData({
                  loginval:1
                })
              }else if(res.sta == 2){
                that.setData({
                  loginval:2
                })
              }else{
                showText('未知错误！', 2)
              }
            })
          }else{
            showText('登录失败请重试！', 2)
          }
        })
      }
    })
  },
  nav(e){
    navTo(e)
  },
  // 手机号
  getPhoneNumber(e){
    const that = this
    wx.login({
      success:(data)=>{
        if(data.code){
          var obj = {
            code:data.code,
            encryptedData:e.detail.encryptedData,
            iv:e.detail.iv,
            uid:wx.getStorageSync('uid')
          }
          getTel(obj).then(confing=>{
            if(confing.sta == 1){
              that.setData({
                'form.tel':confing.msg,
                telbutton:false,
                loginval:2
              })
              showText('绑定成功！',1)
            }else{
              showText('绑定失败！',2)
            }
          })
        }else{
          showText('获取code失败！',2)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var obj = {
      uid:wx.getStorageSync('uid')
    }
    getshopCollectListCount(obj).then(res=>{
      this.setData({
        shop_count:res.msg || 0
      })
    })
    const that = this
    wx.login({
      success(data){
        if(data.code){
          login(data.code).then(res=>{
            if(res.sta == 0){
              that.setData({
                openid:res.openid,
                session_key:res.session_key
              })
              showText('请先登录！',2)
            }else if(res.sta == 1){
              that.setData({
                loginval:1,
                userInfo:res.user
              })
              if(res.uid){
                wx.setStorageSync('uid', res.uid)
              }
              var uid = wx.getStorageSync('uid')
              inqTel(uid).then(res=>{
                if(res.sta == 0){
                  showText('用户信息有误！', 2)
                }else if(res.sta == 1){
                  that.setData({
                    loginval:1
                  })
                  showText('请先绑定手机号！', 2)
                }else if(res.sta == 2){
                  that.setData({
                    loginval:2
                  })
                }else{
                  showText('未知错误！', 2)
                }
              })
            }else{
              showText('发生错误',  2)
            }
          })
        }else{
          wx.showToast({
            icon:'error',
            title:' 登录失败！',
          })
        }
      }
    })
  },

  phoneCall(e) {
    var tel = e.currentTarget.dataset.replyPhone
    wx.makePhoneCall({
      phoneNumber: String(tel),
    })
  },

  getshopFn(){
    var obj = {
      uid:wx.getStorageSync('uid'),
      shop_id:wx.getStorageSync('shop_id')
    }
    getShop(obj).then(res=>{
      console.log(res.admin_arr)
      if(res.admin_arr.indexOf(1) > -1) {
        this.setData({
          all_admin:true
        })
      }
      if(res.admin_arr.indexOf(2)> -1) {
        this.setData({
          admin2:true
        })
      }
      if(res.admin_arr.indexOf(3)> -1) {
        this.setData({
          admin3:true
        })
      }

      if(res.sta == 1){
        if(res.shop.images){
          var arr1 = []
          res.shop.images = res.shop.images.split(',')
          res.shop.images.forEach(item=>{
            arr1.push(baseURL + item)
          })
          res.shop.images = arr1
        }
        this.setData({
          shop_obj:res.shop,
          admin:res.admin_arr
        })
      }else{
        this.setData({
          hotel_show:false
        })
        this.setData({
          loginval:0
        })
        showText('获取门店信息失败！',2)
      }
    })
  },
 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */ 
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var carttotal = wx.getStorageSync('cart').length || 0
    wx.setTabBarBadge({
      index:2,
      text: String(carttotal)
    })
    this.setData({
      cartTotal:carttotal
    })
    var shop_id = wx.getStorageSync('shop_id') || false
    var uid = wx.getStorageSync('uid') || false
    if(shop_id && uid){
      this.getshopFn()
      this.setData({
        hotel_show:true
      })
    }else{
      this.setData({
        hotel_show:false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */

})